package com.eco.fanliapp.job.adminServerTask;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.entity.*;
import com.eco.fanliapp.service.*;
import com.eco.fanliapp.utils.DateFormatUtils;
import com.eco.fanliapp.vo.SyChannelUserVO;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 绑定订单批量处理
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class AdminServerTask implements Job, Serializable {


    private static final Logger log = LoggerFactory.getLogger(AdminServerTask.class);

    @Autowired
    private IAppCompanyService appCompanyService;

    @Autowired
    private IStatisticsService iStatisticsService;

    public AdminServerTask() {}

    @Override
    public void execute(JobExecutionContext context) {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap dataMap = jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long startTime = System.currentTimeMillis();
        log.info("***************************************开始执行统计定时扫描任务-AdminServerTask：executeCount=" + executeCount + "--startTime=" + startTime);


        try {
            List<AppCompany> list = appCompanyService.list();
            for(AppCompany company:list){
                //渠道下总统计
                //handleTotalChannel(company.getCompanyId());
                //渠道下详情统计
                //handleTotalChannelDetail(company.getCompanyId());
                //业务后台统计汇总
                handleTotalStatic(company.getCompanyId());
                //总数据
                handleTotalStaticList();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行统计定时扫描任务错误");
        }

        long endTime = System.currentTimeMillis();
        log.info("***************************************结束执行统计定时扫描任务-AdminServerTask：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime - startTime));

    }
    //渠道下总统计
/*    public void handleTotalChannel(Integer companyId ){
        String redisKey = "manageStatisticalNew_" + DateFormatUtils.getYearsterDateDay() + "_" + companyId;
        //删除昨日缓存
        if(redisTemplateService.hasKey(redisKey)){
            redisTemplateService.del(redisKey);
        }
        //添加缓存
        syChannelService.manageStatisticalNew(companyId, null);
    }*/

    //渠道下详情统计
/*    public void handleTotalChannelDetail(Integer companyId){
        //获取公司下渠道
        QueryWrapper<SyChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", companyId);
        queryWrapper.eq("enable", 1);
        queryWrapper.eq("deleted", 0);
        List<SyChannel> list = syChannelService.list(queryWrapper);

        for(SyChannel channel:list){
            //删除昨日缓存
            Long channelId = channel.getTableId();
            String redisKey = "statisticalDetailNew_" + DateFormatUtils.getYearsterDateDay() + "_" + channelId;
            if(redisTemplateService.hasKey(redisKey)){
                redisTemplateService.del(redisKey);
            }
            //添加缓存
            syChannelService.statisticalDetailNew(channelId);
        }
    }*/
    //业务后台统计汇总
    public void handleTotalStatic(Integer companyId){
        //添加缓存
        iStatisticsService.jsonListTask(companyId);
        for(int type=0; type<7; type++){
            iStatisticsService.jsonTabListTask(companyId,type);
        }
    }
    //所有数据统计
    public void handleTotalStaticList(){
        //添加缓存
        iStatisticsService.jsonListTask(null);
        for(int type=0; type<7; type++){
            iStatisticsService.jsonTabListTask(null,type);
        }
    }

}
